package com.halloon.android.task;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.halloon.android.R;
import com.halloon.android.data.ContentManager;
import com.halloon.android.data.SettingsManager;

public class PostActionTask extends AsyncTask<Void, Void, String> {
	private Context context;
	
	public static final int COMMENT = 0;
	public static final int RETWEET = 1;
	public static final int PUBLISH_TWEET = 2;
	public static final int PUBLISH_IMAGE_TWEET = 3;
	
	private int type;
	private String tweetId;
	private String content;
	private String longitude;
	private String latitude;
	private String syncFlag;
	private String imagePath;
	
	private String ip;
	
	private String[] tickerState;
	private String[] tickerString;
	
	private NotificationManager ntfManager;
	
	/**
	 * 
	 * @param context
	 * @param type 类型 (COMMENT,RETWEET,PUBLISH_TWEET,PUBLISH_IMAGE_TWEET)
	 * @param tweetId 回复及转发的 微博ID
	 * @param content 内容
	 * @param longitude 经度（可选）
	 * @param latitude 纬度(可选）
	 * @param syncFlag 是否同步 0x0同步 0x1不同步
	 * @param imagePath 图片路径 （可选）
	 */
	public PostActionTask(Context context, int type, String tweetId, String content, String longitude, String latitude, String syncFlag, String imagePath){
		this.context = context;
		this.type = type;
		this.tweetId = tweetId;
		this.content = content;
		this.longitude = longitude;
		this.latitude = latitude;
		this.syncFlag = syncFlag;
		this.imagePath = imagePath;
		ntfManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		tickerState = new String[]{context.getString(R.string.sending), context.getString(R.string.sent)};
		tickerString = new String[]{context.getString(R.string.comment), context.getString(R.string.retweet), ""};
		ip = SettingsManager.getInstance(context).getDeviceIp();
	}
	
	public PostActionTask(Context context, int type, String content, String syncFlag, String imagePath){
		this.context = context;
		this.type = type;
		this.content = content;
		this.syncFlag = syncFlag;
		this.imagePath = imagePath;
		ntfManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		tickerState = new String[]{context.getString(R.string.sending), context.getString(R.string.sent)};
		tickerString = new String[]{context.getString(R.string.comment), context.getString(R.string.retweet), ""};
		ip = SettingsManager.getInstance(context).getDeviceIp();
	}
	
	public PostActionTask(Context context, int type, String content){
		this.context = context;
		this.type = type;
		this.content = content;
		ntfManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		tickerState = new String[]{context.getString(R.string.sending), context.getString(R.string.sent)};
		tickerString = new String[]{context.getString(R.string.comment), context.getString(R.string.retweet), ""};
		ip = SettingsManager.getInstance(context).getDeviceIp();
	}
	
	public PostActionTask(Context context, int type, String tweetId, String content){
		this.context = context;
		this.type = type;
		this.tweetId = tweetId;
		this.content = content;
		ntfManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		tickerState = new String[]{context.getString(R.string.sending), context.getString(R.string.sent)};
		tickerString = new String[]{context.getString(R.string.comment), context.getString(R.string.retweet), ""};
		ip = SettingsManager.getInstance(context).getDeviceIp();
	}
	
	public PostActionTask(Context context, int type, String content, String longitude, String latitude, String syncFlag, String imagePath){
		this.context = context;
		this.type = type;
		this.content = content;
		this.longitude = longitude;
		this.latitude = latitude;
		this.syncFlag = syncFlag;
		this.imagePath = imagePath;
		ntfManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		tickerState = new String[]{context.getString(R.string.sending), context.getString(R.string.sent)};
		tickerString = new String[]{context.getString(R.string.comment), context.getString(R.string.retweet), ""};
		ip = SettingsManager.getInstance(context).getDeviceIp();
	}
	
	@Override
	protected void onPreExecute(){
		Notification notification = new Notification();
		notification.icon = R.drawable.ic_launcher;
		notification.tickerText = tickerState[0] + tickerString[type];
		notification.when = System.currentTimeMillis();
		PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(), 0);
		notification.setLatestEventInfo(context, tickerState[0], content, intent);
		ntfManager.notify(0, notification);
	}
	
	@Override
	protected String doInBackground(Void... params){
		switch(type){
		case COMMENT:
			ContentManager.getInstance(context).comment(content, tweetId, ip);
			break;
		case RETWEET:
			ContentManager.getInstance(context).retweet(content, tweetId, ip);
			break;
		case PUBLISH_TWEET:
			if(longitude == null){
				ContentManager.getInstance(context).addText(content, ip);
			}else{
				ContentManager.getInstance(context).addText(content, longitude, latitude, ip, syncFlag);
			}
			break;
		case PUBLISH_IMAGE_TWEET:
			if(longitude == null){
				ContentManager.getInstance(context).addImageTweet(content, imagePath, ip);
			}else{
				ContentManager.getInstance(context).addImageTweet(content, imagePath, ip, longitude, latitude, syncFlag);
			}
			break;
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String result){
		Notification notification = new Notification();
		notification.icon = R.drawable.ic_launcher;
		notification.tickerText = tickerState[1];
		notification.when = System.currentTimeMillis();
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(), 0);
		notification.setLatestEventInfo(context, tickerState[1], content, intent);
		ntfManager.notify(0, notification);
	}
	
	public int getType(){
		return type;
	}

}
